//
//  ItemDBTools.m
//  NC-Trendy
//
//  Created by LeungChaos on 16/2/26.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "ItemDBTools.h"

@implementation ItemDBTools

+ (NSManagedObjectContext *)objContext {
    return [AppDelegate shareAppDelegate].managedObjectContext;
}

+ (NSFetchRequest *)fetchRequest {
    return [NSFetchRequest fetchRequestWithEntityName:@"CoreItem"];
}

+ (NSEntityDescription *)entityDesc {
    return [NSEntityDescription entityForName:@"CoreItem" inManagedObjectContext:[self objContext]];
}

+ (NSMutableArray<CoreItem*> *)itemArrayWithType:(NSString *)type withMin_id:(NSString *)min_id errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSMutableArray *mutArray = [NSMutableArray array];
    int i = [min_id intValue];
    while (mutArray.count == 10) {
        i--;
        NSFetchRequest *fetchRequest = [self fetchRequest];
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"item_type = %@ and item_id = %@",type,i];
        NSArray *array = [[self objContext]executeFetchRequest:fetchRequest error:&error];
        if (array.count == 1) {
            [mutArray addObject:[array lastObject]];
        }
    }
    if (errorBlock) {
        errorBlock(error);
    }
    if (mutArray.count > 0) {
        return mutArray;
    } else {
        return nil;
    }
}

+ (void)insertCoreItemWithItem:(NCItem *)item errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSFetchRequest *fetchRequest = [self fetchRequest];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"item_id = %@",item.item_id];
    NSArray *array = [[self objContext]executeFetchRequest:fetchRequest error:&error];
    if (array.count > 0) {
        return;
    } else {
        CoreItem *cItem = [[CoreItem alloc]initWithEntity:[self entityDesc] insertIntoManagedObjectContext:[self objContext]];
        cItem.item_desc = item.item_desc;
        cItem.item_id = item.item_id;
        cItem.item_image = item.item_image;
        cItem.item_title = item.item_title;
        cItem.item_type = item.item_type;
        [[self objContext] insertObject:cItem];
        [[self objContext] save:&error];
        if (errorBlock) {
            errorBlock(error);
        }
    }
}

+ (void)deleteAllCoreItemWithErrorInfo:(void(^)(NSError *error))errorBlock {
    __block NSError *error;
    NSArray<CoreItem *> *array = [[self objContext] executeFetchRequest:[self fetchRequest] error:&error];
    if (array.count > 0) {
        [array enumerateObjectsUsingBlock:^(CoreItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [[self objContext] deleteObject:obj];
            [[self objContext] save:&error];
        }];
    }
    if (errorBlock) {
        errorBlock(error);
    }
}

+ (NCItem *)ncItemWithCoreItem:(CoreItem *)cItem {
    NCItem *item = [[NCItem alloc]init];
    item.item_desc = cItem.item_desc;
    item.item_id = cItem.item_id;
    item.item_image = cItem.item_image;
    item.item_title = cItem.item_title;
    item.item_type = cItem.item_type;
    return item;
}

@end
